package com.badger.spring.cloud.alibaba.sentinel.config;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import feign.FeignException;

/**
 * 全局异常捕获
 * @author liqi
 */
@ControllerAdvice
public class WholeExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = FeignException.class)
    public Map<Integer, Object> feignException(HttpServletRequest req, FeignException e) {
        System.out.println(e.getMessage());
        Map<Integer, Object> dataMap = new HashMap<>();
        dataMap.put(500, e.getMessage());
        e.printStackTrace();
        return dataMap;
    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Map<Integer, Object> exception(HttpServletRequest req, Exception e) {
        System.out.println("我是大异常:" + e.getMessage());
        Map<Integer, Object> dataMap = new HashMap<>();
        dataMap.put(500, e.getMessage());
        e.printStackTrace();
        return dataMap;
    }
}
